package com.zr.sms.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.sms.dao.CashregisterDao;
import com.zr.sms.model.Cashregister;
import com.zr.sms.util.JDBCUtil;

public class CashregisterDaoImpl implements CashregisterDao{

	
	public int getCashCount() {
		int scount = 0;
		Connection   con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select count(cid) scount from cashregister");
		try {
			PreparedStatement   pst =  con.prepareStatement(sql.toString());
			
			ResultSet  rs = pst.executeQuery();
			if(rs.next()) {
				scount = rs.getInt("scount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return scount;
	}
	

	public List<Cashregister> getCash(int start, int pageSize) {
		List<Cashregister> cash = new ArrayList<Cashregister>(); 
		Connection   con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select cid,ccapital,cincome from  cashregister");
		sql.append(" limit ?,?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			
			pst.setInt(1, start);
			pst.setInt(2, pageSize);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Cashregister ca = new Cashregister();
				ca.setCid(rs.getInt("cid"));
				ca.setCcapital(rs.getDouble("ccapital"));
				ca.setCincome(rs.getDouble("cincome"));
				cash.add(ca);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cash;
	}
	
	public double getAllMoney() {
		double allmoney = 0;
		Connection   con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select sum(cincome) as allmoney from cashregister");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				allmoney = rs.getDouble("allmoney");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return allmoney;
	}
	
	public int updateCash(String[] cids) {
		int flag = 0;
		Connection   con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("update cashregister set cincome=0 where ");
		for(int i=0;i<=cids.length-2;i++){
			sql.append(" cid=? or");
		}
		sql.append(" cid=?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			for(int j=0;j<=cids.length-1;j++){
				pst.setInt(j+1, Integer.parseInt(cids[j]));
			}
			int a = pst.executeUpdate();
			if(a>0){
				flag=1;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}


	
	public boolean addMoney(double totalmoney,int cid) {
		 Connection con=null;
		 PreparedStatement stmt=null;
		 boolean flag=false;
		 con=JDBCUtil.getConnection();
		 StringBuffer sql=new StringBuffer("update cashregister set  cincome = cincome+? where cid=? ");
		 try {
			stmt=con.prepareStatement(sql.toString());
			stmt.setDouble(1, totalmoney);
			stmt.setInt(2, cid);
			int a=stmt.executeUpdate();
			if(a>0){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		return flag;
	}


	
	public boolean cutMoney(double chajia, int cid) {
		Connection con=null;
		 PreparedStatement stmt=null;
		 boolean flag=false;
		 con=JDBCUtil.getConnection();
		 StringBuffer sql=new StringBuffer("update cashregister set  cincome = cincome-? where cid=? ");
		 try {
			stmt=con.prepareStatement(sql.toString());
			stmt.setDouble(1, chajia);
			stmt.setInt(2, cid);
			int a=stmt.executeUpdate();
			if(a>0){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		return flag;
	}
}
